package com.zhhh.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.wordnik.swagger.annotations.ApiOperation;
import com.zhhh.domain.BMenuTree;
import com.zhhh.domain.BMode;
import com.zhhh.domain.BModeControl;
import com.zhhh.domain.BModeEm;
import com.zhhh.domain.BModeOper;
import com.zhhh.domain.PageHelper;
import com.zhhh.model.ResultModel;
import com.zhhh.repository.MenuTreeRepository;
import com.zhhh.repository.ModeControlRepository;
import com.zhhh.repository.ModeEmRepository;
import com.zhhh.repository.ModeOperRepository;
import com.zhhh.repository.ModeRepository;

@RestController
@RequestMapping("/Mode")
public class ModeController {

	@Autowired
	private ModeRepository modeRepository;

	@Autowired
	private ModeControlRepository modeControlRepository;

	@Autowired
	private ModeOperRepository modeOperRepository;

	@Autowired
	private ModeEmRepository modeEmRepository;

	@Autowired
	private MenuTreeRepository menuRepository;

	@ResponseBody
	@RequestMapping(value = "/queryModeAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有模块清单")
	public ResponseEntity<PageHelper<BMode>> queryModeAll(@RequestParam Integer size,@RequestParam Integer page) {
		Pageable pageable = PageRequest.of(page, size);
		PageHelper<BMode> pageHelper = new PageHelper<BMode>();  
		Page<BMode> findAll = modeRepository.findAll(pageable);
		Long total = modeRepository.countByAll(); 
		List<BMode> content = findAll.getContent();
		pageHelper.setRows(content);
		pageHelper.setTotal(total);
		return new ResponseEntity<>(pageHelper, HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteMode", method = RequestMethod.POST)
	@ApiOperation(value = "删除模块")
	public ResponseEntity<ResultModel> deleteMode(@RequestParam Integer id) {
		modeRepository.deleteById(id);
		List<BMode> findAll = (List<BMode>) modeRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/updateMode", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者修改模块")
	public ResponseEntity<ResultModel> updateMode(@ModelAttribute("bMode") BMode bMode) {
		modeRepository.save(bMode);
		List<BMode> findAll = (List<BMode>) modeRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/findModeControlById", method = RequestMethod.POST)
	@ApiOperation(value = "查询模块下控件清单")
	public ResponseEntity<List<BModeControl>> findModeControlById(@RequestParam String modeCode) {
		List<BModeControl> findAll = modeControlRepository.findBymodeCode(modeCode);
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}

	@RequestMapping(value = "/updateModeControl2", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者修改模块控件清单")
	public ResponseEntity<ResultModel> updateModeControl2(@ModelAttribute("bMode") BModeControl bModeControl) {
		BModeControl findById = modeControlRepository.findById(bModeControl.getId()).get();
		if (findById == null) {
			findById = new BModeControl();
		}
		findById.setCtrlId(bModeControl.getCtrlId());
		findById.setCtrlText(bModeControl.getCtrlText());
		findById.setModeCode(bModeControl.getModeCode());
		findById.setDataId(bModeControl.getDataId());
		findById.setIfFtp(bModeControl.getIfFtp());
		findById.setReportUrl(bModeControl.getReportUrl());
		modeControlRepository.save(findById);
		List<BModeControl> findAll = modeControlRepository.findBymodeCode(bModeControl.getModeCode());
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteModeControl", method = RequestMethod.POST)
	@ApiOperation(value = "删除模块控件清单")
	public ResponseEntity<ResultModel> deleteModeControl(@RequestParam Integer id, @RequestParam String modeCode) {
		modeControlRepository.deleteById(id);
		List<BModeControl> findAll = modeControlRepository.findBymodeCode(modeCode);
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/findModeOperByCode", method = RequestMethod.POST)
	@ApiOperation(value = "查询模块清单操作功能")
	public ResponseEntity<List<BModeOper>> findModeOperByCode(@RequestParam String modeCode) {
		List<BModeOper> findAll = modeOperRepository.findByctrlCode(modeCode);
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}

	@RequestMapping(value = "/findModeEmByCode", method = RequestMethod.POST)
	@ApiOperation(value = "查询控件详细信息")
	public ResponseEntity<List<BModeEm>> findModeEmByCode(@RequestParam String modeCode) {
		List<BModeEm> findAll = modeEmRepository.findByctrlCode(modeCode);
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}

	@RequestMapping(value = "/updateModeOper", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者修改模块清单操作功能")
	public ResponseEntity<ResultModel> updateModeOper(@ModelAttribute("bModeOper") BModeOper bModeOper) {
		modeOperRepository.save(bModeOper);
		List<BModeOper> findAll = modeOperRepository.findByctrlCode(bModeOper.getCtrlCode());
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/updateEmOper", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者修改控件详细信息")
	public ResponseEntity<ResultModel> updateEmOper(@ModelAttribute("bModeEm") BModeEm bModeEm) {
		modeEmRepository.save(bModeEm);
		List<BModeEm> findAll = modeEmRepository.findByctrlCode(bModeEm.getCtrlCode());
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteModeOper", method = RequestMethod.POST)
	@ApiOperation(value = "删除模块清单操作功能")
	public ResponseEntity<ResultModel> deleteModeOper(@RequestParam Integer id, @RequestParam String modeCode) {
		modeOperRepository.deleteById(id);
		List<BModeOper> findAll = modeOperRepository.findByctrlCode(modeCode);
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteModeEm", method = RequestMethod.POST)
	@ApiOperation(value = "删除控件详细信息")
	public ResponseEntity<ResultModel> deleteModeEm(@RequestParam Integer id, @RequestParam String modeCode) {
		modeEmRepository.deleteById(id);
		List<BModeEm> findAll = modeEmRepository.findByctrlCode(modeCode);
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

	@RequestMapping(value = "/queryMenuAll", method = RequestMethod.POST)
	@ApiOperation(value = "查询所有菜单")
	public ResponseEntity<List<BMenuTree>> queryMenuAll() {
		List<BMenuTree> findAll = (List<BMenuTree>) menuRepository.findAll();
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}

	@RequestMapping(value = "/deleteMenu", method = RequestMethod.POST)
	@ApiOperation(value = "删除菜单")
	public ResponseEntity<List<BMenuTree>> deleteMenu(@RequestParam Integer id) {
		menuRepository.deleteById(id);
		List<BMenuTree> findAll = (List<BMenuTree>) menuRepository.findAll();
		return new ResponseEntity<>(findAll, HttpStatus.OK);
	}

	@RequestMapping(value = "/updateMenu", method = RequestMethod.POST)
	@ApiOperation(value = "增加或者修改菜单")
	public ResponseEntity<ResultModel> updateMenu(@ModelAttribute("menu") BMenuTree menu) {
		menuRepository.save(menu);
		List<BMenuTree> findAll = (List<BMenuTree>) menuRepository.findAll();
		return new ResponseEntity<>(ResultModel.ok(findAll), HttpStatus.OK);
	}

}
